package org.andolphin.client.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import org.andolphin.client.provider.PaymentColumns;
import org.andolphin.client.utils.DateUtils;
import org.andolphin.client.utils.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.database.Cursor;
import android.util.Log;

/**
 * 收支情况
 * 
 * @author matianyi
 */
public class Payment implements JsonSerializable, Serializable {

    public static final String TAG = Payment.class.getName();

    private static final long serialVersionUID = 1L;

    // 序号, 客户端本地数据库用
    private Integer id;

    // 服务端的Key
    private String key;

    // 金额
    private BigDecimal amount;

    // 收/支==VAL_EXPENDITURE/VAL_INCOME
    private String balance;

    // 消费种类
    private String category;

    // 备注
    private String description;

    // 发生日
    private Date eventDate;

    // 最后更新时间
    private Date lastUpdated;

    public static final String VAL_EXPENDITURE = "0";

    public static final String VAL_INCOME = "1";

    public Payment() {
    }

    public Payment(Integer id, BigDecimal amount, String balance, String category, String description, Date eventDate, Date lastUpdated) {
        this.id = id;
        this.amount = amount;
        this.balance = balance;
        this.category = category;
        this.description = description;
        this.eventDate = eventDate;
        this.lastUpdated = lastUpdated;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getEventDate() {
        return eventDate;
    }

    public void setEventDate(Date eventDate) {
        this.eventDate = eventDate;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void fromJSON(JSONObject json) {
        this.id = json.optInt("id");
        this.key = json.optString("key", "");
        this.amount = new BigDecimal(json.optDouble("amount"));
        this.balance = json.optString("balance", VAL_INCOME);
        this.category = json.optString("category", "");
        this.description = json.optString("description", "");
        this.eventDate = DateUtils.parseDate(json.optString("event_date", ""));
        this.lastUpdated = DateUtils.parseDateTime(json.optString("last_updated", ""));
    }

    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        try {
            json.put("id", this.id);
            json.put("key", this.key);
            json.put("amount", this.amount.doubleValue());
            json.put("balance", this.balance);
            json.put("category", this.category);
            json.put("description", this.description);
            json.put("eventDate", DateUtils.formatDateTime(this.eventDate));
            json.put("lastUpdated", DateUtils.formatDateTime(this.lastUpdated));
        } catch (JSONException e) {
            Log.d(TAG, StringUtils.digMessage(e), e);
        }

        return json;
    }

    public void fromCursor(Cursor cursor) {
        this.id = cursor.getInt(cursor.getColumnIndex(PaymentColumns._ID));
        this.amount = new BigDecimal(cursor.getDouble(cursor.getColumnIndex(PaymentColumns.COL_AMOUNT)));
        this.balance = cursor.getString(cursor.getColumnIndex(PaymentColumns.COL_BALANCE));
        this.category = cursor.getString(cursor.getColumnIndex(PaymentColumns.COL_CATEGORY));
        this.description = cursor.getString(cursor.getColumnIndex(PaymentColumns.COL_DESCRIPTION));
        this.eventDate = DateUtils.parseDate(cursor.getString(cursor.getColumnIndex(PaymentColumns.COL_EVENT_DATE)));
        this.lastUpdated = DateUtils.parseDateTime(cursor.getString(cursor.getColumnIndex(PaymentColumns.COL_LAST_UPDATED)));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Payment other = (Payment) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}
